/*
 * Copyright 2017, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ctyon.watchmanager.ui.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.ctyon.watchmanager.R;
import com.ctyon.watchmanager.databinding.ItemPictureBinding;
import com.ctyon.watchmanager.listener.ItemClickListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import static com.ctyon.watchmanager.config.OssConfig.endpoint;
import static com.ctyon.watchmanager.config.OssConfig.picture;

public class PictureAdapter extends RecyclerView.Adapter<PictureAdapter.WatchViewHolder> {

    List<String> mPictureList = new ArrayList<>();

    @Nullable
    private final ItemClickListener onItemClickListener;

    public PictureAdapter(@Nullable ItemClickListener itemClickListener) {
        onItemClickListener = itemClickListener;
        setHasStableIds(true);
    }

    public void setPictureList(final List<String> pictureList) {
        mPictureList.clear();
        mPictureList.addAll(pictureList);
        notifyDataSetChanged();
    }

    public void remove(final int index) {
        Log.d("PictureAdapter", "index = " + index + "  remove size = " + mPictureList.size());
        mPictureList.remove(index);
        if (0 == index){
            notifyDataSetChanged();
        } else {
            notifyItemRemoved(index);
        }
    }

    @Override
    public WatchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d("Lee_PictureAdapter", "onCreateViewHolder");
        ItemPictureBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_picture, parent, false);
        binding.setListener(onItemClickListener);
        return new WatchViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(WatchViewHolder holder, int position) {
        Log.d("Lee_PictureAdapter", "onBindViewHolder");
        Log.e("androidLog", "position=" + position + ";img=" + mPictureList.get(position));
        holder.binding.setPath(endpoint.replace("http://", "https://ctyon.") + "/" + picture + "socketclient" + mPictureList.get(position) + ".jpg");
        holder.binding.setIndex(position);
        holder.binding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        Log.d("Lee_PictureAdapter", "getItemCount");
        return mPictureList == null ? 0 : mPictureList.size();
    }

    @Override
    public long getItemId(int position) {
        Log.d("Lee_PictureAdapter", "getItemId position = " + position);
        return position;
    }

    static class WatchViewHolder extends RecyclerView.ViewHolder {

        final ItemPictureBinding binding;

        public WatchViewHolder(ItemPictureBinding binding) {
            super(binding.getRoot());
            Log.d("Lee_PictureAdapter", "ChatsViewHolder");
            this.binding = binding;
        }
    }
}
